package EventLoopTest.Test002;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class EventLoopBindingExample {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup(2); // 2个EventLoop
        
        // 创建多个 Channel
        for (int i = 0; i < 5; i++) {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
                     .channel(NioSocketChannel.class)
                     .handler(new SimpleChannelInboundHandler<String>() {

                         @Override
                         protected void channelRead0(ChannelHandlerContext ctx, String msg) {
                             // 处理消息
                             EventLoop eventLoop = ctx.channel().eventLoop();
                             System.out.println("当前 EventLoop: " + eventLoop);
                         }
                     });
            
            Channel channel = bootstrap.connect("localhost", 8888).sync().channel();
            
            // 验证线程绑定
            EventLoop eventLoop = channel.eventLoop();
            System.out.println("Channel " + i + " 绑定到: " + eventLoop);
        }
    }
}